package com.hdu.seckilldemo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdu.seckilldemo.exception.GlobalException;
import com.hdu.seckilldemo.mapper.UserMapper;
import com.hdu.seckilldemo.pojo.User;
import com.hdu.seckilldemo.service.IUserService;
import com.hdu.seckilldemo.utils.CookieUtil;
import com.hdu.seckilldemo.utils.MD5Util;
import com.hdu.seckilldemo.utils.UUIDUtil;
import com.hdu.seckilldemo.utils.ValidatorUtil;
import com.hdu.seckilldemo.vo.LoginVo;
import com.hdu.seckilldemo.vo.RespBean;
import com.hdu.seckilldemo.vo.RespBeanEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lwy
 * @since 2023-02-07
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        User user = userMapper.selectById(mobile);
        //没有查到用户
        if(user == null){
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }

        //密码不匹配
        if(!user.getPassword().equals(MD5Util.frontPassToDBPass(password,user.getSalt()))){
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }
        //登录成功生成cookie
        String ticket = UUIDUtil.uuid();
//        request.getSession().setAttribute(ticket,user);
        //将用户数据从写入session改为写入redis
        redisTemplate.opsForValue().set("user:" + ticket, user);
        CookieUtil.setCookie(request,response,"userTicket",ticket);
        return RespBean.success(ticket);
    }

    @Override
    public User getUserByCookie(String userTicket, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isEmpty(userTicket)) {
            return null;
        }
        User user =(User) redisTemplate.opsForValue().get("user:"+userTicket);
        if(user != null){
            CookieUtil.setCookie(request,response,"userTicket",userTicket);
        }
        return user;
    }
    //  更新密码
    @Override
    public RespBean updatePassword(String userTicket, String password,HttpServletRequest request, HttpServletResponse response) {
        //获取用户
        User user = getUserByCookie(userTicket, request, response);
        if(user == null){
            //如果找不到用户就抛异常
            throw new GlobalException(RespBeanEnum.MOBILE_NOTFOUND);
        }
        //更新用户密码
        user.setPassword(MD5Util.inputPassToDBPass(password,user.getSalt()));
        int result = userMapper.updateById(user);
        if(result == 1){
            //更新成功，删除redis中的缓存
            redisTemplate.delete("user:"+userTicket);
            return RespBean.success();
        }
        // 否则 返回更新失败
        return RespBean.error(RespBeanEnum.PASSWORD_UPDATE_FAIL);
    }
}
